package com.lyj.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lyj.mall.consts.MallConst;
import com.lyj.mall.dao.CategoryMapper;
import com.lyj.mall.pojo.Category;
import com.lyj.mall.service.CategoryService;
import com.lyj.mall.vo.CategoryVo;
import com.lyj.mall.vo.ResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lyj
 * @date 2019/11/26 23:09
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Category record) {
        return categoryMapper.insert(record);
    }

    @Override
    public int insertSelective(Category record) {
        return categoryMapper.insertSelective(record);
    }

    @Override
    public Category selectByPrimaryKey(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Category record) {
        return categoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Category record) {
        return categoryMapper.updateByPrimaryKey(record);
    }

    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
        List<Category> categoryList = categoryMapper.selectAll();
        List<CategoryVo> categoryVoList = categoryList.stream()
                .filter(category -> category.getParentId().equals(MallConst.ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                .collect(Collectors.toList());
        categoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
        findSubCategorys(categoryVoList, categoryList);
        return ResponseVo.success(categoryVoList);
    }

    @Override
    public Set<Integer> findSubCategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> categoryList = categoryMapper.selectAll();
        findSubCategoryId(id, resultSet, categoryList);
        return resultSet;
    }

    private void findSubCategoryId(Integer id, Set<Integer> resultSet, List<Category> categoryList) {
        for (Category category : categoryList) {
            if (category.getParentId().equals(id)) {
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(), resultSet);
            }
        }
    }

    private CategoryVo category2CategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtil.copyProperties(category, categoryVo);
        return categoryVo;
    }

    private void findSubCategorys(List<CategoryVo> categoryVoList, List<Category> categoryList) {
        categoryVoList.forEach(categoryVo -> {
            List<CategoryVo> subCategorys = categoryList.stream().filter(category ->
                    categoryVo.getId().equals(category.getParentId())
            ).map(this::category2CategoryVo).collect(Collectors.toList());
            // 使用比较器进行排序
            subCategorys.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());

            categoryVo.setSubCategorys(subCategorys);
            findSubCategorys(subCategorys, categoryList);
        });
    }
}
